//
//  VideoPlayerViewController.m
//  MyVocalNovels
//
//  Created by mac on 2019/4/20.
//  Copyright © 2019 YangYang. All rights reserved.
//

#import "VideoPlayerViewController.h"
#import "AppDelegate.h"

@interface VideoPlayerViewController ()
@property (nonatomic, strong) UIView *playBgView;
@property (nonatomic, strong) VLCMediaPlayer *player;
@property (nonatomic, strong) VLCMedia *media;
@end

@implementation VideoPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    
    _playBgView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight / 2 - 100, kScreenWidth, 200)];
    [self.view addSubview:_playBgView];
    _playBgView.backgroundColor = [UIColor lightGrayColor];
    
    _player = [[VLCMediaPlayer alloc] init];
    NSString *videoURLString = [_videoURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    _media = [VLCMedia mediaWithURL:[NSURL URLWithString:videoURLString]];
    _player.media = _media;
    _player.drawable = _playBgView;
    [_player play];
}

#pragma mark ------------orientChangeNotification 监听横竖屏切换--------------
- (void)orientChange:(NSNotification *)notification{
    
    UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (interfaceOritation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"横屏；");
            _playBgView.frame = CGRectMake(0 , 0, kScreenWidth, kScreenHeight);
            self.navigationController.navigationBarHidden = YES;
            break;
        case UIInterfaceOrientationPortrait:
            NSLog(@"竖屏:向上");
            _playBgView.frame = CGRectMake(0, kScreenHeight / 2 - 100, kScreenWidth, 200);
            self.navigationController.navigationBarHidden = NO;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            NSLog(@"竖屏:向下");
            _playBgView.frame = CGRectMake(0, kScreenHeight / 2 - 100, kScreenWidth, 200);
            self.navigationController.navigationBarHidden = NO;
            break;
        case UIInterfaceOrientationUnknown:
            NSLog(@"不清楚状态");
            break;
        default:
            break;
    }
    if (interfaceOritation == UIInterfaceOrientationLandscapeRight) {
        
        
    }else if(interfaceOritation == UIInterfaceOrientationPortrait){
        NSLog(@"竖屏");
        _playBgView.frame = CGRectMake(0, kScreenHeight / 2 - 100, kScreenWidth, 200);
        self.navigationController.navigationBarHidden = NO;
        
        //        [exitFullScreen removeFromSuperview];
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotate = YES;
    
    PlayerBottomView *bottomView = [PlayerBottomView shareInstance];
    [UIView animateWithDuration:0.5f animations:^{
        bottomView.frame = CGRectMake(0, kScreenHeight + 10, kScreenWidth, CGRectGetHeight(bottomView.frame));
    } completion:^(BOOL finished) {
        
    }];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotate = NO;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
